package com.reliable.service.controller;

import com.reliable.api.dto.session.SessionIpDetailDTO;
import com.reliable.api.dto.session.SessionIpDetailPageDTO;
import com.reliable.api.param.session.SessionIpDetailPageParam;
import com.reliable.common.core.controller.BaseController;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.service.domain.ReliableSessionIpDetail;
import com.reliable.service.service.IReliableSessionIpOccurrencesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuyantao
 * ClassName: ReliableSessionIpOccurrencesController
 * date: 2025/11/12 17:07
 */
@RestController
@RequestMapping("/reliable/sessionIpOccurrences")
@Api(value = "ip控制器", tags = {"ip管理"})
public class ReliableSessionIpOccurrencesController extends BaseController {

    @Autowired
    private IReliableSessionIpOccurrencesService sessionIpOccurrencesService;

    /**
     * 查询ip列表,分页，带查询条件
     */
    @PostMapping("/listDetail")
    @ApiOperation("分页查询会话IP详细信息列表")
    public AjaxResult listDetail(@RequestBody SessionIpDetailPageParam param) {
        // 查询数据
        List<ReliableSessionIpDetail> details = sessionIpOccurrencesService.selectSessionIpDetailList(
                param.getSessionId(),
                param.getIp(),
                param.getCity(),
                param.getStartTime(),
                param.getEndTime(),
                param.getPageNum().intValue(),
                param.getPageSize().intValue()
        );

        // 转换为DTO
        List<SessionIpDetailDTO> dtoList = details.stream().map(detail -> {
            SessionIpDetailDTO dto = new SessionIpDetailDTO();
            BeanUtils.copyProperties(detail, dto);
            return dto;
        }).collect(Collectors.toList());

        // 构造分页结果
        SessionIpDetailPageDTO pageDTO = new SessionIpDetailPageDTO();
        pageDTO.setRecords(dtoList);
        pageDTO.setPageNum(param.getPageNum());
        pageDTO.setPageSize(param.getPageSize());
        pageDTO.setTotal((long) dtoList.size());

        return AjaxResult.success(pageDTO);
    }
}